package com.dotflat.physicalevaluate.service.impl;


import com.dotflat.physicalevaluate.domain.pe.PERecord;
import com.dotflat.physicalevaluate.domain.pe.RelRecordMeasure;
import com.dotflat.physicalevaluate.service.EvaluateConclusionService;
import com.matt.foundation.utils.DateUtil;
import com.tuo51.foudation.utils.EvaluationReportUtils;
import com.tuo51.foudation.utils.export.HealthReportUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EvaluateConclusionServiceImpl implements EvaluateConclusionService {

    @Override
    public String rankDesc(int fullScore){
        String rankVal = "仍需努力";
        if (fullScore >= 30){
            rankVal = "优秀";
        }else if (fullScore >= 24){
            rankVal = "良好";
        }else if(fullScore >= 18){
            rankVal = "正常";
        }
        return rankVal;
    }

    @Override
    public String getComplexConclusion(String name, int fullScore){

        int level = 4;
        String rankVal = "仍需努力";
        if (fullScore >= 30){
            rankVal = "优秀";
            level = 1;
        }else if (fullScore >= 24){
            rankVal = "良好";
            level = 2;
        }else if(fullScore >= 18){
            rankVal = "正常";
            level = 3;
        }

        String template = "本次测量综合得分%d分，较同龄宝宝而言处于%s水平，请继续保持。";

        if (level == 4){
            template = "本次测量综合得分%d分，较同龄宝宝而言处于%s水平，有轻度发育不良迹象，需调整饮食和增强锻炼";
        }
        String ret = String.format(template, fullScore, rankVal);
        return ret;
    }

    @Override
    public String getNormalConclusion(PERecord record) {

        List<RelRecordMeasure> A = new ArrayList<>();
        List<RelRecordMeasure> Ap = new ArrayList<>();
        List<RelRecordMeasure> Am = new ArrayList<>();

        for(int i = 0 ; i < record.getRels().size(); i++){
            RelRecordMeasure c = record.getRels().get(i);

            if (c.getRankVal() == 3){
                //正常项目
                A.add(c);
            }else if(c.getRankVal() > 3){
                //优势项目
                Ap.add(c);
            }else if(c.getRankVal() <= 2){
                //劣势项目
                Am.add(c);
            }
        }

        final String apStr = "非常好，较同龄宝宝而言处于优势水平，请继续保持或可探索宝宝的特长培养；";
        final String aStr = "处于正常水平，请保持关注；";
        final String amStr = "相对较弱，需进一步加强，家长应适当干预，并咨询老师或专家获取相关提升方案；";

        StringBuilder tipStr = new StringBuilder();
        tipStr.append("从测评结果来看，"+record.getChildrenName() + "宝宝");

        generateConclusionPart(tipStr, Ap, apStr);
        generateConclusionPart(tipStr, A, aStr);
        generateConclusionPart(tipStr, Am, amStr);

        if(tipStr.substring(tipStr.length() - 1).equals("；")){
            tipStr.replace(tipStr.length() - 1 , tipStr.length(), "。");
        }

        return tipStr.toString();
    }


    private static final String TIPSTR_dd = "能区发育";
    private static final String TIPSTR_all = "皆";

    private void generateConclusionPart(StringBuilder c, List<RelRecordMeasure> ds, String tail){


        if(ds.size() >0){
            c.append(concatDimensionScores(ds));
            if (ds.size()>1){
                c.append(int2Chinese(ds.size()));
            }
            c.append(TIPSTR_dd);
            if(ds.size() == 5){
                c.append(TIPSTR_all);
            }
            c.append(tail);
        }
    }

    private String int2Chinese(int a){
        if(a < 0 || a > 5) a = 0;
        final String res[] = {"","一个","两个","三个","四个","五个"};
        return res[a];
    }


    private String concatDimensionScores(List<RelRecordMeasure>scores){
        String ret = "";

        for (RelRecordMeasure score : scores){
            if(ret.length() > 0){
                ret += "、";
            }
            int value = score.getRankVal();
            ret += String.format("%s（%d分）", score.getDimension(), value);
        }

        return ret;
    }

    @Override
    public Map generateReportExtInfo(PERecord record){
        Map result = new HashMap();

        result.put("id", record.getId());   //?????

        //if(StringUtils.isBlank(warningConclusion)) {
        result.put("normalConclusion", getNormalConclusion(record));


        result.put("bmi", HealthReportUtil.bmiInfo(record.getChildrenName(), record.getGender(), DateUtil.parseToDate(record.getBirthday()), record.getWeight(), record.getHeight()));

        //下次测评时间
        result.put("nextEvaluatingDate", EvaluationReportUtils.nextEvaluateDate(record.getEvaluatingDate(), record.getMonth()));

        return result;
    }
}
